package com.jinghang.wayofrich.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.jinghang.wayofrich.MainActivity;
import com.jinghang.wayofrich.R;
import com.jinghang.wayofrich.common.CustomRequest;
import com.jinghang.wayofrich.common.ToastUtil;
import com.jinghang.wayofrich.common.UrlConstant;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 登录页
 */
public class LoginActivity extends AppCompatActivity{
    private EditText mobile;
    private EditText smsCode;
    private TimeCount time;
    private Button loginButton;
    private Button sendSms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mobile = findViewById(R.id.login_mobile);
        smsCode = findViewById(R.id.login_smscode);

        mobile.setText("18810528823");
        smsCode.setText("888888");

        //返回按钮事件
        LinearLayout returnBack = findViewById(R.id.login_return_back);
        returnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginActivity.this.finish();
            }
        });

        //登录按钮
        loginButton = findViewById(R.id.login_button);
        time = new TimeCount(10000, 1000);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //校验手机号是否填写
                if(mobile.getText() == null || "".equals(mobile.getText().toString())){
                    ToastUtil.showToast(getApplicationContext(),"请输入手机号");
                    return;
                }
                ;

                //校验手机号是否填写
                if(smsCode.getText() == null || "".equals(smsCode.getText().toString())){
                    ToastUtil.showToast(getApplicationContext(),"请输入短信验证码");
                    return;
                }
                ;

                RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                String url = UrlConstant.getReuqestUrl(UrlConstant.URL_USER_LOGIN);
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("phonenumber",mobile.getText().toString());
                hashMap.put("authCode",smsCode.getText().toString());
                CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST,
                        url, hashMap, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.d("登录结果", jsonObject.toString());
                        try{
                            String code = jsonObject.getString("code");
//                            if(!"0".equals(code)){
//                                ToastUtil.showToast(getApplicationContext(),jsonObject.getString("message"));
//                                return;
//                            }
                            Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
                            LoginActivity.this.startActivity(mainIntent);
                            LoginActivity.this.finish();
                        }catch (Exception e){

                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("请求出错", volleyError.getMessage());
                    }
                });
                requestQueue.add(jsonObjectRequest);
            }
        });


        //微信登录
        ImageView wechatLogin = findViewById(R.id.login_wechat);
        wechatLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(getApplicationContext(),"微信登录开发中");
            }
        });

        //获取短信验证码
        sendSms = findViewById(R.id.login_sendsms);
        sendSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //校验手机号是否填写
                if(mobile.getText() == null || "".equals(mobile.getText().toString())){
                    ToastUtil.showToast(getApplicationContext(),"请输入手机号");
                    return;
                }
                ;
                RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                String url = UrlConstant.getReuqestUrl(UrlConstant.URL_GET_SMSCODE);
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("phonenumber",mobile.getText().toString());
                hashMap.put("type","1");
                CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST,
                        url, hashMap, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.d("获取验证码结果", jsonObject.toString());
                        time.start();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("请求出错", volleyError.getMessage());
                    }
                });
                requestQueue.add(jsonObjectRequest);
            }
        });

    }

    /**
     * 验证码获取后倒计时
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            sendSms.setBackgroundResource(R.drawable.button_grey);
            sendSms.setClickable(false);
            sendSms.setText("("+millisUntilFinished / 1000 +") ");
        }
        @Override
        public void onFinish() {
            sendSms.setText("重新获取验证码");
            sendSms.setClickable(true);
            sendSms.setBackgroundResource(R.drawable.button_rectangle);
        }
    }
}
